// recommendPackage/pages/recommendDetail/recommendDetail.js
import request from '../../../util/request'
import PubSub from 'pubsub-js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recommendDetailList:[],//推荐歌曲列表
    img:'',//推荐图片
    text:'',//推荐文本
    index:0
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        success: () => {
          wx.reLaunch({
            url: '/pages/login/login',
          })
        }
      })
    }
    this.getRecommendDetail(options.recommendId)
    PubSub.subscribe('switchTypeRecommend', (_, type) => {
      let {
        recommendDetailList,
        index
      } = this.data
      if (type === 'pre') {
        (index === 0) && (index = recommendDetailList.length)
        index -= 1;
      } else {
        (index === recommendDetailList.length - 1) && (index = -1)
        index += 1;
      }
      this.setData({
        index
      })
      let musicId = recommendDetailList[index].id
      PubSub.publish('musicRecommend', musicId)
    })
  },
  // 获取推荐列表歌单
 async getRecommendDetail(recommendId){
    let recommendData=await request('/playlist/detail',{id:recommendId});
    this.setData({
      img:recommendData.playlist.coverImgUrl,
      recommendDetailList:recommendData.playlist.tracks,
      text:recommendData.playlist.name
    })
  },
  //跳转到播放页面
  toSongDetail(event){
    let {
      item,
      index
    } = event.currentTarget.dataset
    this.setData({
      index
    })
    let title =0
    //路由跳转传参 query参数
    wx.navigateTo({
      // url: '/pages/songDetail/songDetail?song='+JSON.stringify(song),
      url: '/songPackage/pages/songDetail/songDetail?musicId=' + item.id+'&title='+title


    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})